<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2007-2008 Starbugs (univis2typo3@googlegroups.com)
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*  A copy is found in the textfile GPL.txt and important notices to the license
*  from the author is found in LICENSE.txt distributed with these scripts.
*
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
 * Module: Web>UnivIS2Typo3
 *
 * Description:
 *
 * Notice: 
 *
 *  
 * @author	Starbugs <univis2typo3@googlegroups.com>
 */

require_once($BACK_PATH.'template.php');
require_once(t3lib_extMgm::extPath('univis2typo3').'/mod1/configuration/class.createLecture.php');

class ConfigurationLectures
{
	var $content;
	var $template;
	var $lecture;
	var $semesters;
	var $BACK_PATH = '../../../../typo3/';

	function renderContent($id)
	{			
		$this->template = new Template();
		$this->lecture = new CreateLecture();
		$content = '';
		$output = '';
		
		if (empty($_GET['tab'])) { $_GET['tab'] = 1; }
		
		
		if ($_GET['name'])
		{
			ob_start();
			$this->lecture->setLectureKey($_GET['name']);
			$this->lecture->setParentPageId($_GET['id']);
			$this->lecture->setSemester($_GET['semester']);
			$this->lecture->create();
			$output = ob_get_contents();
			ob_end_clean();
			$content .= "
				<script type='text/javascript'>
					top.content.nav_frame.refresh_nav();
				</script>
		";
			
		}
		$content .= '<img'.t3lib_iconWorks::skinImg($this->BACK_PATH,'gfx/button_down.gif','width="11" height="10"').'></img>';
		
		$content .= "<p>To create pages for a lecture just click the CREATE PAGE button next to it.</p>";
		
		$content .= $this->getSemesterTabMenu($id);

		$content .= $_GET['name'];
		$content .= '<br />'.$output; 
		
		return $content;
	}

	function getSemesterTabMenu($id)
	{
		$selection  = "DISTINCT `semester` ";
		$from		= "`u_lecture`";
		$where		= "";
		$groupby	= "";
		$orderby	= "`semester` DESC";
		$limit 		= "";

		$query = $GLOBALS['TYPO3_DB']->SELECTquery(
			$selection,   			// SELECT
			$from,					// FROM
			$where,					// WHERE
			$groupby,	    		// GROUP BY
			$orderby,				// ORDER BY
			$limit					// LIMIT
		);
		$res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);
		$out = array ();
	
		echo mysql_error();
		while ($row = mysql_fetch_assoc($res)) 
		{
			$out[] = $row['semester'];
		}		

		$script = basename(PATH_thisScript);
		$activeTab = 1;
		if (!empty($_GET['tab'])) { $activeTab = $_GET['tab']; }
		
		$menuDef = array();
		$tabId=0;
		$tabId++;

                $output = $this->template->header("Lectures with NO page");
		$output .= '1) Select semester:';

		foreach($out as $semester)
		{
                        $menuDef[$tabId] = array(
                                'label' => t3lib_div::deHSCentities(htmlspecialchars($semester)),
                                'content' => '2) Select lecture:'.$this->getLectures($id,$semester) //$this->template->table($this->getLectures($id,$semester))
                        );

//			$menuDef[$tabId] = array(
//							'isActive'=>($tabId == $activeTab),
//							'label'=>t3lib_div::deHSCentities(htmlspecialchars($semester)),
//							'url'=>htmlspecialchars($script.'?id='.$id).'&'.htmlspecialchars('tab='.$tabId),
//					);
			$tabId++;
		}
		
		$this->semester = $out;
//		return $this->template->getTabMenuRaw($menuDef);
		$output .= $this->template->getDynTabMenuJScode();
		$output .= $this->template->getDynTabMenu($menuDef, 'selectSemester', 0, false, 25, 1, false, 1);
		return $output;
	}
	
	function getLectures($id, $semester)
	{
		$selection  = "DISTINCT `name`, `semester`, `key`, `type`, `title` ";
		$from		= "`u_lecture`
						LEFT OUTER JOIN `be_groups` ON CONCAT_WS('.', 'Lecture', `u_lecture`.`semester`, `u_lecture`.`short`) = `be_groups`.`title`
					";
		$where		= "ISNULL(`title`) AND `semester` = '".$semester."' AND `type` <> 'ue'";
		$groupby	= "";
//		$orderby	= "`u_person_job_order`.`sorting`, `u_person`.`lastname`";
		$orderby	= "";
		$limit 		= "";

		$query = $GLOBALS['TYPO3_DB']->SELECTquery(
			$selection,   			// SELECT
			$from,					// FROM
			$where,					// WHERE
			$groupby,	    		// GROUP BY
			$orderby,				// ORDER BY
			$limit					// LIMIT
		);
		$res = $GLOBALS['TYPO3_DB']->sql(TYPO3_db, $query);
		$out = array ();

		echo mysql_error();
		$out[] = array("<b>Name</b>", "<b>Semester</b>", "&nbsp;");
		while ($row = mysql_fetch_assoc($res)) 
		{
//			$createLink = "<b><a href='?name=".$row['key']."&id=".$id."' style='background-color:black; color:white'>---CREATE PAGE---</a></b>";
			$createLink = $this->template->t3Button("document.location='?name=".$row['key']."&id=".$id."&semester=".$row['semester']."'","CREATE PAGE");
			$out[] = array($row['name'],$row['semester'],$createLink);
		}		
		
		$this->template->table_TABLE = '<table border="1" cellspacing="0" cellpadding="3" id='.$id.'>';		
		return $this->template->table($out);
	}
}
?>
